<template>
    <div class="box">
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username"  placeholder="手机号"
                    :rules="[{ pattern: telpattern, required: true, message: '请填写正确的手机号' }]" />
                <van-field v-model="password" :type="checked?'text':'password'" name="password"  placeholder="密码"
                    :rules="[{ pattern: pwdpattern, required: true, message: '请填写正确的密码' }]" >
                    <template #button>
                        <van-switch v-model="checked" />
                    </template>
                </van-field>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit" style="border-radius: 10px;background: red;border: none;">
                    登录
                </van-button>
            </div>
        </van-form>

        <div style="float: left;">
            <van-icon name="sign" />
            <span>忘记密码</span>
        </div>
        <div style="float: right;">
            <van-icon name="sign" />
            <router-link to="/eig">快速注册</router-link>
        </div>

        
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { loginApi } from '@/api/api';
import { showToast } from 'vant';
const checked = ref(true);
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()
const username = ref('');
const password = ref('');
const onSubmit = (values) => {
    console.log('submit', values);
    loginApi(username.value, password.value).then(res => {
        if (res.data.code == 0) {
            localStorage.setItem('token', res.data.data.token)
            router.push('/home/index')
            showToast('登录成功');
        }
        else {
            showToast(res.data.meg);
        }
    })
};


const telpattern = /^1[3-9]\d{9}$/;
const pwdpattern = /^\d{6,6}$/;

</script>

<style lang="scss" scoped></style>